package com.hongtech.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hongtech.common.bean.AjaxResultVo;
import com.hongtech.framework.anno.OperateLog;
import com.hongtech.framework.anno.Permission;
import com.hongtech.modules.common.BaseController;
import com.hongtech.modules.system.dto.SysMenuDTO;
import com.hongtech.modules.system.entity.SysMenuEntity;
import com.hongtech.modules.system.service.SysMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 角色控制器
 */
@Controller
@RequestMapping("/system/sysmenu")
public class MenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 跳转图标页
     */
    @Permission("system:sysmenu:list")
    @RequestMapping("/icon")
    public String icon() {
        return "system/menu/icon";
    }

    /**
     * 跳转菜单页
     */
    @Permission("system:sysmenu:list")
    @RequestMapping("/list")
    public String list() {
        return "system/menu/list";
    }

    /**
     * 分页查询菜单
     */
    @Permission("system:sysmenu:list")
    @RequestMapping("/page")
    @ResponseBody
    public List<SysMenuDTO> page(String search) {
        LambdaQueryWrapper<SysMenuEntity> ew = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(search)) {
            ew.like(SysMenuEntity::getMenuName, search);
        }
        List<SysMenuDTO> menuList = sysMenuService.list(ew);
        for (SysMenuDTO menu : menuList) {
            if (menu.getPid() == null || menu.getDeep() != 3) {
                String folder = "<i class='" + menu.getIcon() + "'></i> ";
                menu.setMenuName(StringUtils.join(folder, menu.getMenuName()));
            } else {
                String file = "<i class='" + menu.getIcon() + "'></i> ";
                menu.setMenuName(StringUtils.join(file, menu.getMenuName()));
            }
        }
        return menuList;
    }

    /**
     * 增加菜单
     */
    @Permission("system:sysmenu:save")
    @RequestMapping("/add")
    public String add(Model model) {
        LambdaQueryWrapper<SysMenuEntity> ew = new LambdaQueryWrapper<>();
        ew.eq(SysMenuEntity::getPid, 0L);
        ew.orderByAsc(SysMenuEntity::getSort);
        List<SysMenuDTO> list = sysMenuService.list(ew);
        model.addAttribute("list", list);
        return "system/menu/add";
    }

    /**
     * 添加目录
     */
    @Permission("system:sysmenu:save")
    @OperateLog("创建目录菜单")
    @RequestMapping("/doAddDir")
    @ResponseBody
    public AjaxResultVo doAddDir(SysMenuDTO sysMenuDTO) {
        sysMenuDTO.setPid(0L);
        sysMenuDTO.setDeep(1);
        sysMenuService.add(sysMenuDTO);
        return AjaxResultVo.success();
    }

    /**
     * 添加菜单
     */
    @Permission("system:sysmenu:save")
    @OperateLog("创建菜单")
    @RequestMapping("/doAddMenu")
    @ResponseBody
    public AjaxResultVo doAddMenu(SysMenuDTO sysMenuDTO) {
        sysMenuDTO.setDeep(2);
        sysMenuService.add(sysMenuDTO);
        return AjaxResultVo.success();
    }

    @Permission("system:sysmenu:save")
    @OperateLog("新增功能菜单")
    @RequestMapping("/doAddAction")
    @ResponseBody
    public AjaxResultVo doAddAction(SysMenuDTO sysMenuDTO) {
        sysMenuDTO.setDeep(3);
        sysMenuService.add(sysMenuDTO);
        return AjaxResultVo.success();
    }

    /**
     * 编辑菜单
     */
    @Permission("system:sysmenu:edit")
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        SysMenuDTO sysMenuDTO = sysMenuService.get(id);
        model.addAttribute("sysMenu", sysMenuDTO);
        LambdaQueryWrapper<SysMenuEntity> ew = new LambdaQueryWrapper<>();
        ew.eq(SysMenuEntity::getPid, 0L);
        ew.orderByAsc(SysMenuEntity::getSort);
        if (sysMenuDTO.getDeep() == 1) {
            return "/system/menu/editDir";
        } else if (sysMenuDTO.getDeep() == 2) {
            List<SysMenuDTO> list = sysMenuService.list(ew);
            model.addAttribute("list", list);
            return "/system/menu/editMenu";
        } else {
            List<SysMenuDTO> list = sysMenuService.list(ew);
            model.addAttribute("list", list);
            model.addAttribute("pSysMenu", sysMenuService.getById(sysMenuDTO.getPid()));
            return "/system/menu/editAction";
        }
    }

    /**
     * 执行编辑菜单
     */
    @Permission("system:sysmenu:edit")
    @OperateLog("编辑菜单")
    @RequestMapping("/doEdit")
    @ResponseBody
    public AjaxResultVo doEdit(SysMenuDTO sysMenuDTO) {
        sysMenuService.update(sysMenuDTO);
        return AjaxResultVo.success();
    }

    /**
     * 执行编辑菜单
     */
    @Permission("system:sysmenu:remove")
    @OperateLog("删除菜单")
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResultVo delete(Long id) {
        sysMenuService.delete(Lists.newArrayList(id));
        return AjaxResultVo.success();
    }

    /**
     * 根据父节点获取子菜单
     */
    @RequestMapping("/listMenuByPid")
    @ResponseBody
    public AjaxResultVo listMenuByPid(Long pid) {
        LambdaQueryWrapper<SysMenuEntity> ew = new LambdaQueryWrapper<>();
        ew.eq(SysMenuEntity::getPid, pid);
        ew.orderByAsc(SysMenuEntity::getSort);
        List<SysMenuDTO> list = sysMenuService.list(ew);
        List<Map<String, Object>> listMap = Lists.newArrayList();
        for (SysMenuDTO m : list) {
            Map<String, Object> map = Maps.newHashMap();
            map.put("id", m.getId());
            map.put("text", m.getMenuName());
            listMap.add(map);
        }
        return AjaxResultVo.success(listMap);
    }

    /**
     * 验证菜单资源名称是否存在
     */
    @RequestMapping("/checkMenuResource")
    @ResponseBody
    public AjaxResultVo checkMenuResource(String permissions) {
        LambdaQueryWrapper<SysMenuEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenuEntity::getPermissions, permissions);
        List<SysMenuDTO> list = sysMenuService.list(queryWrapper);
        if (list.size() > 0) {
            return AjaxResultVo.error(permissions + " 资源已存在,请换一个尝试");
        }
        return AjaxResultVo.success();
    }

}
